<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryForm" class="demo-form-inline">
      <el-form-item label="用户名称">
        <el-input v-model="queryForm.userName" placeholder="用户名称"></el-input>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="queryForm.roleId" multiple clearable placeholder="角色">
          <el-option v-for="item in roleList" :key="item.roleId" :label="item.roleName" :value="item.roleId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryForm.status" clearable placeholder="用户状态">
          <el-option v-for="item in statusList" :key="item.value" :label="item.name" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="queryData">查询</el-button>
      </el-form-item>
    </el-form>
    <hl-table ref="HlTable" :data-url="dataUrl" :query-form="queryForm" :button-list="buttonList" @add="addData" @delete="deleteData">
      <el-table-column type="selection" align="center" width="55"></el-table-column>
      <el-table-column align="center" label="ID" prop="userId" width="55"></el-table-column>
      <el-table-column label="账户" width="100" prop="accountName"></el-table-column>
      <el-table-column label="用户名称" width="100" prop="userName"></el-table-column>
      <el-table-column label="性别" width="55" prop="sexName" align="center"></el-table-column>
      <el-table-column label="手机号" prop="phone" align="center"></el-table-column>
      <el-table-column label="邮件地址" prop="mail" align="center"></el-table-column>
      <el-table-column label="角色" align="center" prop="roleName" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column align="center" prop="createTime" label="创建时间" width="100"></el-table-column>
      <el-table-column label="状态" align="center" width="80" prop="created_at">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusFilter">{{ scope.row.statusName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          <el-button size="mini" type="danger" @click="resetPassWord(scope.$index, scope.row)">重置密码</el-button>
        </template>
      </el-table-column>
    </hl-table>
    <hl-dialog :dialog-show="dialogVisible" width="40%" :title="title" @handleClose="handleClose" @handleSave="handleSave">
      <user-add ref="userRef" :dialog-show="dialogVisible" :user-id="userId"></user-add>
    </hl-dialog>
  </div>
</template>

<script>
import HlTable from '@/components/HlTable/index'
import HlDialog from '@/components/HlDialog/index'
import UserAdd from '@/views/user/UserAdd'
export default {
  components: { UserAdd, HlTable, HlDialog },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      dataUrl: '/frontend/api/user/tableList',
      buttonList: [
        { type: 'primary', icon: 'el-icon-plus', click: 'add', clickName: '新增' },
        { type: 'danger', icon: 'el-icon-delete', click: 'delete', clickName: '删除' }
      ],
      queryForm: {
        userName: '',
        roleId: [],
        status: ''
      },
      title: '',
      statusList: [],
      roleList: [],
      dialogVisible: false,
      userId: ''
    }
  },
  created() {
    // 获取用户状态
    this.getStatus()
    // 获取角色信息
    this.getRoleList()
  },
  methods: {
    handleSizeChange(val) {
      this.$message.success(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.$message.success(`当前页: ${val}`)
    },
    // 编辑
    handleEdit(index, item) {
      this.title = '修改用户'
      this.userId = item.userId
      this.dialogVisible = true
    },
    queryData() {
      this.$refs.HlTable.getTable()
    },
    getStatus() {
      this.$http.get('/frontend/api/enum/getEnum', { catalog: 'COMMON', type: 'STATUS' }).then(res => {
        this.statusList = res
      }).catch(errer => {
        this.$message.error(errer.message)
      })
    },
    getRoleList() {
      this.$http.get('/frontend/api/role/roleList').then(res => {
        this.roleList = res
      }).catch(errer => {
        this.$message.error(errer.message)
      })
    },
    // 删除
    handleDelete(index, item) {
      this.$confirm('此操作将永久删除该用户，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const userIds = [item.userId]
        this.$http.post('/frontend/api/user/delete', { userIds: userIds }).then(res => {
          this.$message.success('删除成功')
          this.queryData()
        }).catch(errer => {
          this.$message.error(errer.message)
        })
      }).catch(() => {
      })
    },
    addData(item) {
      // 跳转其他页面
      this.dialogVisible = true
      this.userId = 0
      this.title = '新增用户'
    },
    // 关闭新增窗口
    handleClose() {
      this.dialogVisible = false
    },
    // 确定窗口
    handleSave() {
      this.$refs.userRef.save(() => {
        this.$refs.HlTable.getTable()
        this.$message.success('保存成功')
        this.dialogVisible = false
      })
    },
    deleteData(item) {
      console.info('dataData', item)
      console.info('dataData', item.length)
      if (item.length <= 0) {
        this.$message.error('请选择需要删除的数据')
      } else {
        this.$confirm('此操作将永久删除该用户，是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const userIds = []
          for (let i = 0; i < item.length; i++) {
            userIds.push(item[i].userId)
          }
          this.$http.post('/frontend/api/user/delete', { userIds: userIds }).then(res => {
            this.$message.success('删除成功')
            this.queryData()
          }).catch(errer => {
            this.$message.error(errer.message)
          })
        }).catch(() => {
        })
      }
    },
    // 重置密码
    resetPassWord(index, item) {
      const param = { userId: item.userId }
      this.$http.get('/frontend/api/user/resetPassWord', param).then(res => {
        this.$message.success(res)
      }).catch(errer => {
        this.$message.error(errer.message)
      })
    }
  }
}
</script>
